package com.ymatou.promotion.api.testcase;

import java.io.IOException;
import java.util.Date;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.InvalidCouponBean;
import com.ymatou.promotion.api.service.InvalidCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 作废优惠券接口
 *
 * @author chenjiazhu
 */
@TestSuite
public class Ts_InvalidCoupon {
    private static InvalidCouponBean invalidcouponBean;
    private static InvalidCouponCall invalidcouponCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("作废优惠券接口");
    }

    @Before
    public void caseUp() {
        invalidcouponBean = new InvalidCouponBean();
        invalidcouponCall = new InvalidCouponCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(invalidcouponCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_001() {
        Logger.start(true, "作废全场优惠券");
        try {

            String batchcode = setInvalidCouponBean(1);
        	invalidcouponBean.setBatchCode(batchcode);

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            //verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_002() {
        Logger.start(true, "作废商品优惠券");
        try {

            String batchcode = setInvalidCouponBean(2);

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_003() {
        Logger.start(true, "作废全场优惠券-洋码头PC端");
        try {
            String batchcode = setInvalidCouponBean(1, "1");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_004() {
        Logger.start(true, "作废全场优惠券-洋码头WAP端");
        try {

            String batchcode = setInvalidCouponBean(1, "2");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);


        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_005() {
        Logger.start(true, "作废全场优惠券-洋码头APP");
        try {

            String batchcode = setInvalidCouponBean(1, "3");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_006() {
        Logger.start(true, "作废全场优惠券-神器APP");
        try {

            String batchcode = setInvalidCouponBean(1, "4");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_007() {
        Logger.start(true, "作废全场优惠券-神器WAP");
        try {

            String batchcode = setInvalidCouponBean(1, "5");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_008() {
        Logger.start(true, "作废商品优惠券-洋码头PC端");
        try {

            String batchcode = setInvalidCouponBean(2, "1");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_009() {
        Logger.start(true, "作废商品优惠券-洋码头WAP端");
        try {

            String batchcode = setInvalidCouponBean(2, "2");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_010() {
        Logger.start(true, "作废商品优惠券-洋码头APP");
        try {

            String batchcode = setInvalidCouponBean(2, "3");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_011() {
        Logger.start(true, "作废商品优惠券-神器APP");
        try {

            String batchcode = setInvalidCouponBean(2, "4");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_012() {
        Logger.start(true, "作废商品优惠券-神器WAP");
        try {

            String batchcode = setInvalidCouponBean(2, "5");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_013() {
        Logger.start(true, "作废已全部领用全场优惠券，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(1, null);

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_014() {
        Logger.start(true, "作废已全部领用商品优惠券，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(2, null);

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_015() {
        Logger.start(true, "作废已全部领用全场优惠券-洋码头PC端，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(1, "1");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_016() {
        Logger.start(true, "作废已全部领用全场优惠券-洋码头WAP端，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(1, "2");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_017() {
        Logger.start(true, "作废已全部领用全场优惠券-洋码头APP，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(1, "3");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_018() {
        Logger.start(true, "作废已全部领用全场优惠券-神器APP，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(1, "4");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_019() {
        Logger.start(true, "作废已全部领用全场优惠券-神器WAP，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(1, "5");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_020() {
        Logger.start(true, "作废已全部领用商品优惠券-洋码头PC端，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(2, "1");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_021() {
        Logger.start(true, "作废已全部领用商品优惠券-洋码头WAP端，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(2, "2");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_022() {
        Logger.start(true, "作废已全部领用商品优惠券-洋码头APP，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(2, "3");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_023() {
        Logger.start(true, "作废已全部领用商品优惠券-神器APP，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(2, "4");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_024() {
        Logger.start(true, "作废已全部领用商品优惠券-神器WAP，作废为0张");
        try {

            String batchcode = setInvalidCouponBean2(2, "5");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "2", "2", "0");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_025() {
        Logger.start(true, "作废已作废商品优惠券");
        try {

            String batchcode = setInvalidCouponBean(1);

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            // 在作废一次
            invalidcouponCall.callService();

            // 验证返回
            Logger.verifyEquals(ECode.ERROR500, invalidcouponCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, invalidcouponCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(ECode.NullException_STRING, invalidcouponCall.getMsg(), "验证返回Msg");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_026() {
        Logger.start(false, "验证BatchCode空时报错");
        try {

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            // 验证返回
            Logger.verifyEquals(ECode.ERROR500, invalidcouponCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, invalidcouponCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(ECode.NullException_STRING, invalidcouponCall.getMsg(), "验证返回Msg");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_027() {
        Logger.start(false, "验证BatchCode不存在时报错");
        try {

            invalidcouponBean.setBatchCode("123");
            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            // 验证返回
            Logger.verifyEquals(ECode.ERROR500, invalidcouponCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, invalidcouponCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(ECode.NullException_STRING, invalidcouponCall.getMsg(), "验证返回Msg");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_028() {
        Logger.start(true, "作废部分领用全场优惠券-洋码头PC端，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(1, "1");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_029() {
        Logger.start(true, "作废部分领用全场优惠券-洋码头WAP端，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(1, "2");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_030() {
        Logger.start(true, "作废部分领用全场优惠券-洋码头APP，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(1, "3");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_031() {
        Logger.start(true, "作废部分领用全场优惠券-神器APP，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(1, "4");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_032() {
        Logger.start(true, "作废部分领用全场优惠券-神器WAP，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(1, "5");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_033() {
        Logger.start(true, "作废部分领用商品优惠券-洋码头PC端，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(2, "1");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_034() {
        Logger.start(true, "作废部分领用商品优惠券-洋码头WAP端，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(2, "2");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_035() {
        Logger.start(true, "作废部分领用商品优惠券-洋码头APP，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(2, "3");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_036() {
        Logger.start(true, "作废部分领用商品优惠券-神器APP，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(2, "4");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_InvalidCoupon_037() {
        Logger.start(true, "作废部分全部领用商品优惠券-神器WAP，，作废为剩余未领用张数");
        try {

            String batchcode = setInvalidCouponBean3(2, "5");

            invalidcouponCall.setData(invalidcouponBean);
            invalidcouponCall.callService();

            verifySuccessReturn(batchcode, "3", "2", "1");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private String setInvalidCouponBean(int scenarioType, String usePlatforms, Boolean isRecieve, int maxSendNum,
            int userMaxReceiveNum) throws ClientProtocolException, IOException
    {
        CouponCallService couponCall = new CouponCallService();

        CouponBatchBean couponBatchBean =
                couponCall.setCouponBatchBean(scenarioType, usePlatforms, maxSendNum, userMaxReceiveNum);
        String batchCode = couponCall.createSellerCoupon(couponBatchBean).getBatchCode();

        if (isRecieve)
        {
            new CouponCallService().receiveCouponInfo(batchCode, Integer.parseInt(EnvSetup.getData("BuyerId")));
            new CouponCallService().receiveCouponInfo(batchCode, Integer.parseInt(EnvSetup.getData("BuyUserId")));
        }

        invalidcouponBean.setBatchCode(batchCode);

        return batchCode;
    }

    /**
     * 设置未领用的Bean
     * 
     * @param scenarioType
     * @param usePlatforms
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    private String setInvalidCouponBean(int scenarioType, String usePlatforms) throws ClientProtocolException,
            IOException
    {
        return setInvalidCouponBean(scenarioType, usePlatforms, false, 2, 1);
    }

    /**
     * 设置全部领用的Bean
     * 
     * @param scenarioType
     * @param usePlatforms
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    private String setInvalidCouponBean2(int scenarioType, String usePlatforms) throws ClientProtocolException,
            IOException
    {
        return setInvalidCouponBean(scenarioType, usePlatforms, true, 2, 1);
    }

    /**
     * 设置部分领用的Bean
     * 
     * @param scenarioType
     * @param usePlatforms
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    private String setInvalidCouponBean3(int scenarioType, String usePlatforms) throws ClientProtocolException,
            IOException
    {
        return setInvalidCouponBean(scenarioType, usePlatforms, true, 3, 1);
    }

    private String setInvalidCouponBean(int scenarioType) throws ClientProtocolException, IOException
    {
        return setInvalidCouponBean(scenarioType, null, false, 2, 1);
    }

    private void verifySuccessReturn(String batchcode, String total, String recieve, String invalid)
    {
        // 验证返回
        Logger.verifyEquals(ECode.SUCCESS, invalidcouponCall.getCode(), "验证返回code");
        Logger.verifyEquals(ECode.BCODE0, invalidcouponCall.getBCode(), "验证返回Bcode");

        Logger.verifyEquals("作废优惠券成功，本批次优惠券共计" + total + "张，已领取" + recieve + "张，作废" + invalid + "张",
                invalidcouponCall.getMessageText(), "验证返回MessageText");
        Logger.verifyEquals(batchcode, invalidcouponCall.getBatchCode(), "验证返回BatchCode正确");

        Logger.verifyEquals(total, invalidcouponCall.getTotalCouponNum(), "验证返回TotalCouponNum正确");
        Logger.verifyEquals(recieve, invalidcouponCall.getReceiveNum(), "验证返回ReceiveNum正确");
        Logger.verifyEquals(invalid, invalidcouponCall.getInvalidNum(), "验证返回InvalidNum正确");

        // 验证数据库
        YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(batchcode).get(0);
        Logger.verifyEquals(true, couponBatch.getbInvalidStatus(), "验证数据库中YmtCouponBatch表中bInvalidStatus字段正确");
        Logger.verifyEquals(true, checkDateNew(couponBatch.getdInvalidTime()), "验证数据库中YmtCouponBatch表中dInvalidTime字段正确");
    }

    private void verifySuccessReturn(String batchcode)
    {
        verifySuccessReturn(batchcode, "2", "0", "2");
    }

    private boolean checkDateNew(Date date)
    {
        Date now = new Date();
        if (date.getYear() != now.getYear() && date.getMonth() != now.getMonth() && date.getDate() != now.getDate()
                && date.getHours() != now.getHours() && Math.abs(date.getMinutes() - now.getMinutes()) > 3)
        {
            return false;
        }
        return true;
    }
}
